from django.shortcuts import render
from .GetData import OpenTaobao

from django.shortcuts import render
from .GetData import OpenTaobao,GetGoodsMSG
from  .data_analysis import SingleDataAnalysis as SDA
from app.models import TemporaryURL,Product
from django.views.decorators.cache import never_cache
import time
import random
@never_cache
def gd_view(request):
    if request.method == 'POST':
        data = request.POST.get('data')  # 获取提交的数据
        OpenTaobao(data)

        Product.objects.all().delete()
        urls_ = TemporaryURL.objects.values_list('url', flat=True)
        # 将urls转换为数组
        url_array = list(urls_)
        print(len(url_array))
        for i in url_array:
            try:
                random_time = random.uniform(1, 3)
                name,name2,price,sales_volume,addr,product_description,seller_services,logistics_services = GetGoodsMSG(i)
                product = Product(
                    name=name,
                    shop_name=name2,
                    sold=price,
                    sales_volume=sales_volume,
                    location=addr,
                    rating_1=product_description,
                    rating_2=seller_services,
                    rating_3=logistics_services
                )
                product.save()
                print('保存')
                time.sleep(random_time)
            except Exception as err:
                time.sleep(5)   #给点时间让我拉验证码
                print(err)

    return render(request, 'gd.html')

from itertools import zip_longest

def zip_products_and_temporaryurls(products, temporaryurls):
    for product, temporaryurl in zip_longest(products, temporaryurls):
        yield product, temporaryurl

'''
def data_view(request):
    products = Product.objects.all()  # 获取所有的Product对象
    temporaryurl = TemporaryURL.objects.all()
    zipped_data = zip_products_and_temporaryurls(products, temporaryurl)
    context = {'zipped_data': zipped_data}
    return render(request, 'data.html', context)
'''

def data_view(request):
    products = Product.objects.all()
    temporaryurls = TemporaryURL.objects.all()  # 假设TemporaryURL与Product有关联关系，可以根据需要调整
    zipped_data = zip(products, temporaryurls)
    context = {'zipped_data': zipped_data}
    return render(request, 'data.html', context)

from django.shortcuts import render
from django.http import HttpResponse
from pyecharts.charts import Bar, Line, Grid
from pyecharts import options as opts

from django.shortcuts import render
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from django.http import HttpResponse

def Visualization(request):
    products = Product.objects.all()
    temporaryurls = TemporaryURL.objects.all()  # Assuming TemporaryURL is related to Product, adjust as needed

    zipped_data = zip(products, temporaryurls)

    name_score, sold_score, three_score, sales_score, show_name_score, normalized_numbers = SDA()  # Assuming this function returns the required scores

    # Extract data for visualization
    shop_names = [product.name for product, _ in zipped_data]
    sold_scores = [score for score in sold_score]
    three_scores = [score for score in three_score]
    sales_scores = [score for score in sales_score]
    normalized_numbers = [number for number in normalized_numbers]

    # Create a bar chart
    bar = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
        .add_xaxis(shop_names)
        .add_yaxis("售价评分", sold_scores)
        .add_yaxis("三项评分", three_scores)
        .add_yaxis("销量评分", sales_scores)
        .add_yaxis("综合评价", normalized_numbers)
        .set_global_opts(title_opts=opts.TitleOpts(title="淘宝商品综合评分"))
    )

    # Add links to each bar
    # 在视图函数中生成数据
    shop_name_links = []

    # 为每个柱状图添加链接
    for i, shop_name in enumerate(shop_names):
        shop_name_links.append({'index': i, 'url': temporaryurls[i].url})
    context = {
        'bar_embed': bar.render_embed(),
        'shop_name_links': shop_name_links,
    }
    return render(request, 'ksh.html', context)

